from django.views import View
# 导入返回响应
from SaaSDevelTool.data_return_response import create_response
# 导入错误信息装饰器
from SaaSDevelTool.data_return_response import error_message_detection
# 查询当前参数是否是int类型
from SaaSDevelTool.data_operation import is_number
# 获取分页器
from SaaSDevelTool.data_operation import paging_tool_join

"""展示用户"""


class SaaSDevelUserRbacQuery(View):
    @error_message_detection
    def get(self, request):
        """展示用户信息"""
        # 获取当前页码
        page_number = request.GET.get("page_number")
        # 获取当前每页展示的数据条数
        page_size = request.GET.get("page_size")
        # 接收名称
        user_name = request.GET.get("user_name")
        # 获取分组编号
        role_id = request.GET.get("role_id")
        # 判断用户组编号
        if not role_id:
            return create_response(code=400, msg="未传递用户组编号。")

        # 判断参数是否是int类型
        if not is_number(page_number) and not is_number(page_size):
            return create_response(code=400, msg="页码于条数必须是数字类型。")

        # 判断user_name是否存在
        if user_name:
            # 定义sql语句
            where_user_name_sql = """ AND u.user_name LIKE '%{}%' """.format(user_name)
        else:
            where_user_name_sql = """"""

        # 定义查询语句
        query_base_info_user_table_sql = """
             SELECT 
                u.user_id,
                u.user_name,
                u.user_mobile,
                DATE_FORMAT(u.create_time,'%Y-%m-%d %H:%i:%s') as create_time,
                DATE_FORMAT(u.update_time,'%Y-%m-%d %H:%i:%s') as update_time,
                biut.user_name as create_user,
                biut2.user_name as update_user, 
                u.user_status 
            FROM 
                db_center_rbac.base_info_user_table u
            LEFT JOIN 
                db_center_rbac.base_info_user_table biut 
            ON
                u.create_user = biut.user_id 
            LEFT JOIN  
                db_center_rbac.base_info_user_table biut2 
            ON
                u.update_user = biut2.user_id
            WHERE 
                u.user_role_id = {}  {}
            ORDER BY  
                 u.user_id
            DESC 
            ;""".format(role_id, where_user_name_sql)
        # 调用分页器
        user_message_data = paging_tool_join(query_base_info_user_table_sql, int(page_number), int(page_size))
        # 返回对应的数据
        return create_response(code=200, msg="", data=user_message_data)
